//{{{

if(typeof(window["TiddlyAddonVersuch"]) != "undefined"){
    TiddlyAddonVersuch.log("plugin already loaded");
} else {

    // MODULE: integrate with addon

    var TiddlyAddonVersuchInterface = {

        loaded: function(){
            this.msg = document.createElement("TiddlyAddonVersuchDataElement"),
            this.evt = document.createEvent("Events"),
            document.documentElement.appendChild(this.msg);
        },
        log: function(s){
            this.logger.log(s);
        },
        err: function(ex){
            this.logger.err(ex);
        },
        dispatch: function(){
            this.evt.initEvent("TiddlyAddonVersuchEvent", true, false);
            this.msg.setAttribute("result", "");//sicherheitshalber
            this.msg.dispatchEvent(this.evt);
            return this.msg.getAttribute("result");
        },
        checkAddon: function(){
            this.msg.setAttribute("cmd", "check");
            this.msg.setAttribute("result", " no-addon ");
            var res = this.dispatch();
            this.log("Checking- Addon kann: " + res);
            return res;
        },
        loadByAddon: function(){
            if (this.checkAddon().indexOf(" load ") == -1) {
                this.log("kein lokaler load");
                return false;
            }
            this.msg.setAttribute("cmd", "load");
            var result = this.dispatch();
            this.log("result: " + result);
            if(result == "loaded")
                return this.msg.getAttribute("data");
            else return false;
        },
        saveByAddon: function(html,dump){
            if (this.checkAddon().indexOf(" save ") == -1) {
                this.log("kein lokaler save");
                return false;
            }
            this.msg.setAttribute("data", html);
            this.msg.setAttribute("dump", dump);
            //senden
            this.msg.setAttribute("cmd", "save");
            var result = this.dispatch(this.evt);
            this.log(result);
            return result == "saved";
        }
    }

    // MODULE: integrate with tiddly

    var TiddlyAddonVersuch = {

        addon: TiddlyAddonVersuchInterface,

        loaded: function(){
            try{
                this.addon.logger = this;
                this.addon.loaded();
            }catch(e){
                this.err(e);
            }
        },
        setConsole: function(){
            this.console = document.getElementById('console');
        },
        log: function (s){
            s = "page: " + s;
            this.console = document.getElementById('console');
            if(!this.console) {
            //alert("keine console, log: " + s);
            } else{
                s=s.split("&").join("&amp;").split( "<").join("&lt;").split(">").join("&gt;");
                this.console.innerHTML += "<br>" + s;
            }
        },
        err: function(catched){
            alert("page: " + catched);
            this.log("CATCHED: " + catched + "\n" + catched.stack);
        },
        saveFile: function(fileUrl,content)
        {

            TiddlyAddonVersuch.log("------saving: " + fileUrl);
            if(fileUrl != getLocalPath(""+document.location) && TiddlyAddonVersuchInterface.checkAddon() != "") {
                TiddlyAddonVersuch.log("------kann noch keine backups");
                return false;
            }

            var r = TiddlyAddonVersuchInterface.saveByAddon(content);
            if(!r) {
                TiddlyAddonVersuch.log("Addon-save ging nicht, probiere naechstes");
                r = mozillaSaveFile(fileUrl,content);
            }
            if(!r)
                r = ieSaveFile(fileUrl,content);
            if(!r)
                r = javaSaveFile(fileUrl,content);
            return r;
        },

        loadFile: function(fileUrl)
        {

            TiddlyAddonVersuch.log("------loading: " + fileUrl);

            var r = TiddlyAddonVersuchInterface.loadByAddon();
            if((r == null) || (r == false)) {
                TiddlyAddonVersuch.log("Addon-load ging nicht, probiere naechstes");
                r = mozillaLoadFile(fileUrl);
            }
            if((r == null) || (r == false))
                r = ieLoadFile(fileUrl);
            if((r == null) || (r == false))
                r = javaLoadFile(fileUrl);
            return r;
        }

    }
    TiddlyAddonVersuch.loaded();


}

//MODULE: replace original functions.
//Tu das immer.
//Weil die im prebody ueberschrieben werden, muss plugin nochmal.

TiddlyAddonVersuch.log("reloaded and rehooking plugin");

function saveFile(fileUrl,content){
    TiddlyAddonVersuch.log("??");
    return TiddlyAddonVersuch.saveFile(fileUrl, content);
}

function loadFile(fileUrl){
    return TiddlyAddonVersuch.loadFile(fileUrl);
}

//}}}